<template>
  <div class="login-wrap">
    <div class="ms-login">
      <div class="login-container">
        <div class="ms-title">智能融合指挥系统</div>
        <el-form
          :model="param"
          :rules="rules"
          ref="login"
          class="ms-content"
          label-position="top"
          :hide-required-asterisk="true"
        >
          <el-form-item label="用户名" prop="username">
            <input
              class="form-input"
              type="text"
              v-model="param.username"
              placeholder="用户名"
            />
          </el-form-item>
          <el-form-item label="密码" prop="password">
            <input
              class="form-input"
              type="password"
              v-model="param.password"
              placeholder="密码"
              @keyup.enter="submitForm"
            />
          </el-form-item>
          <div class="login-btn">
            <div class="fa fa-spinner fa-spin loading" v-if="loading"></div>
            <div
              class="btn"
              type="primary"
              @click="submitForm"
              :disabled="canLoading"
            ></div>
          </div>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive, watch } from 'vue'
import { useStore } from 'vuex'
import { useRoute, useRouter } from 'vue-router'

// 路由
const route = useRoute()
const router = useRouter()
const redirect = ref(undefined) // 登录前所在页面
watch(
  route,
  () => {
    redirect.value = (route.query && route.query.redirect) || '/'
  },
  { immediate: true }
)
const handleRoute = () => (redirect.value === '/404' ? '/' : redirect.value)

// 清除标签
const store = useStore()

// 登录
const param = reactive({
  username: '',
  password: ''
})

const rules = {
  username: [
    {
      required: true,
      message: '请输入用户名'
    }
  ],
  password: [{ required: true, message: '请输入密码' }]
}
const login = ref(null)
const loading = ref(false)
const submitForm = () => {
  // eslint-disable-next-line consistent-return
  login.value.validate(async (valid) => {
    if (valid) {
      loading.value = true
      const isLogin = await store.dispatch('user/login', {
        username: param.username,
        password: param.password
      })
      // 登录成功跳转
      if (isLogin) {
        router.push(handleRoute())
      }
      setTimeout(() => {
        loading.value = false
      }, 500)
    } else {
      return false
    }
  })
}

</script>

<style scoped lang="scss">
.login-wrap {
  position: relative;
  width: 100%;
  height: 100%;
  background-size: 100%;
  background-image: url('@/assets/image/login_bk.png');
  background-repeat: no-repeat;
}
.ms-title {
  width: 100%;
  line-height: 50px;
  text-align: center;
  font-size: 20px;
  color: #fff;
  font-weight: bold;
  padding-top: 10px;
}
.ms-login {
  position: absolute;
  left: 266px;
  top: 300px;
  width: 390px;
  border-radius: 20px;
  background: rgba(177, 197, 225, 0.2);
  overflow: hidden;
  box-shadow: 0px 0px 5px rgba(0, 0, 0, 0.1);
  box-sizing: border-box;
  padding: 8px 6px;
}

.login-container {
  width: 100%;
  height: 100%;
  background: #3a4d68;
  border-radius: 20px;
}
.ms-content {
  padding: 20px 30px 40px;
}
.login-btn {
  text-align: center;
  margin-top: 50px;
  position: relative;
  .loading {
    position: absolute;
    left: 39%;
    top: 8px;
    z-index: 10;
    color: #fff;
  }
  .btn {
    width: 100%;
    height: 32px;
    margin-bottom: 10px;
    background: rgba(55, 69, 96, 0.9);
    color: #dedede !important;
    border: none;
    background-size: 100%;
    background-image: url('@/assets/image/login_button_bk.png');
    background-repeat: no-repeat;
    cursor: pointer;
  }
}
.form-input {
  background: rgba(87, 104, 127, 1);
  border: none;
  outline: none;
  color: #fff;
  font-size: 14px;
  width: 100%;
  height: 40px;
  line-height: 40px;
  box-sizing: border-box;
  padding: 0 10px;
  border-radius: 4px;
  &:focus {
    border: none;
    outline: none;
  }
}
input.form-input::placeholder {
  color: #999;
}
</style>
<style>
.ms-content .el-form-item__label {
  color: #fff !important;
}
</style>
